import maya.OpenMaya as OpenMaya
import maya.OpenMayaAnim as OpenMayaAnim

import masdevallia.utility.Dag as Dag



'''
num_vertex = 3
num_polygon = 1
num_polygon_connects = 3 * num_polygon

vertex_array = OpenMaya.MFloatPointArray()
vertex_array.setLength( num_vertex )
vertex_array.set( OpenMaya.MFloatPoint( 1, 1, 1 ), 0 )
vertex_array.set( OpenMaya.MFloatPoint( -1, -1, 1 ), 1 )
vertex_array.set( OpenMaya.MFloatPoint( 1, -1, 1 ), 2 )

polygon_count = OpenMaya.MIntArray()
polygon_count.setLength( num_polygon )
polygon_count.set( 3, 0 )

polygon_connects = OpenMaya.MIntArray()
polygon_connects.setLength( num_polygon_connects )
polygon_connects.set( 0, 0 )
polygon_connects.set( 1, 1 )
polygon_connects.set( 2, 2 )

mesh_fn = OpenMaya.MFnMesh()
mesh_fn.create( num_vertex, num_polygon, vertex_array, polygon_count, polygon_connects )
'''

'''
3. CHA_BDY_Skull_Body_01
            4. hull: 12
            4. uvs: [0, 0, 0.33333, 0, 0.66666, 0, 1, 0, 1, 0.33333, 1, 0.66666, 1, 1, 0.66666, 1, 0.33333, 1, 0, 1, 0, 0.66666, 0, 0.33333, 0.33333, 0.33333, 0.33333, 0.66666, 0.66666, 0.33333, 0.66666, 0.66666]
            4. vertices: [2, 2, 803.01, 315.72, 0.06695, 5, 507.2, 303.37, 0.93304, 1, 5, 503.08, 82.07, 1, 1, 5, 498.96, -139.21, 1, 1, 5, 494.85, -360.51, 1, 2, 2, 497.01, -348.27, 0.3676, 5, 188.9, -354.82, 0.63239, 2, 2, 191.01, -348.27, 0.74704, 5, -117.04, -349.13, 0.25295, 1, 2, -114.98, -348.27, 1, 1, 2, -114.98, -126.93, 1, 1, 2, -114.98, 94.39, 1, 1, 2, -114.98, 315.72, 1, 2, 2, 191.01, 315.72, 0.72471, 5, -104.69, 314.75, 0.27528, 2, 2, 497.01, 315.72, 0.4048, 5, 201.25, 309.06, 0.59519, 2, 2, 497.01, 94.39, 0.36016, 5, 197.13, 87.76, 0.63984, 2, 2, 191.01, 94.39, 0.74703, 5, -108.8, 93.45, 0.25296, 2, 2, 497.01, -126.93, 0.36759, 5, 193.02, -133.52, 0.6324, 2, 2, 191.01, -126.93, 0.75447, 5, -112.92, -127.83, 0.24552]
            4. height: 918
            4. width: 664
            4. edges: [0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 16, 16, 18, 18, 20, 20, 22, 22, 0]
            4. type: skinnedmesh
            4. triangles: [15, 14, 4, 14, 3, 4, 11, 1, 12, 12, 1, 2, 14, 2, 3, 11, 0, 1, 12, 2, 14, 13, 12, 14, 10, 11, 12, 10, 12, 13, 13, 14, 15, 15, 4, 5, 9, 10, 13, 8, 13, 15, 9, 13, 8, 7, 15, 5, 8, 15, 7, 7, 5, 6]
'''

#vertices = [345.13, 161.13, 347.61, -154.85, -193.36, -159.1, -195.85, 156.88]
#triangles = [1, 3, 0, 1, 2, 3]
#num_vertex = len(vertices) / 2
#num_polygon = 2
#num_polygon_connects = 3 * num_polygon

# panda orig
#vertices = [2, 2, 803.01, 315.72, 0.06695, 5, 507.2, 303.37, 0.93304, 1, 5, 503.08, 82.07, 1, 1, 5, 498.96, -139.21, 1, 1, 5, 494.85, -360.51, 1, 2, 2, 497.01, -348.27, 0.3676, 5, 188.9, -354.82, 0.63239, 2, 2, 191.01, -348.27, 0.74704, 5, -117.04, -349.13, 0.25295, 1, 2, -114.98, -348.27, 1, 1, 2, -114.98, -126.93, 1, 1, 2, -114.98, 94.39, 1, 1, 2, -114.98, 315.72, 1, 2, 2, 191.01, 315.72, 0.72471, 5, -104.69, 314.75, 0.27528, 2, 2, 497.01, 315.72, 0.4048, 5, 201.25, 309.06, 0.59519, 2, 2, 497.01, 94.39, 0.36016, 5, 197.13, 87.76, 0.63984, 2, 2, 191.01, 94.39, 0.74703, 5, -108.8, 93.45, 0.25296, 2, 2, 497.01, -126.93, 0.36759, 5, 193.02, -133.52, 0.6324, 2, 2, 191.01, -126.93, 0.75447, 5, -112.92, -127.83, 0.24552]
#triangles = [15, 14, 4, 14, 3, 4, 11, 1, 12, 12, 1, 2, 14, 2, 3, 11, 0, 1, 12, 2, 14, 13, 12, 14, 10, 11, 12, 10, 12, 13, 13, 14, 15, 15, 4, 5, 9, 10, 13, 8, 13, 15, 9, 13, 8, 7, 15, 5, 8, 15, 7, 7, 5, 6]

vertices = [2, 2, 803.01, 315.72, 0.06695, 5, 507.2, 303.37, 0.93304, 1, 5, 503.08, 82.07, 1, 1, 5, 498.96, -139.21, 1, 1, 5, 494.85, -360.51, 1, 2, 2, 497.01, -348.27, 0.3676, 5, 188.9, -354.82, 0.63239, 2, 2, 191.01, -348.27, 0.74704, 5, -117.04, -349.13, 0.25295, 1, 2, -114.98, -348.27, 1, 1, 2, -114.98, -126.93, 1, 1, 2, -114.98, 94.39, 1, 1, 2, -114.98, 315.72, 1, 2, 2, 191.01, 315.72, 0.72471, 5, -104.69, 314.75, 0.27528, 2, 2, 497.01, 315.72, 0.4048, 5, 201.25, 309.06, 0.59519, 2, 2, 497.01, 94.39, 0.36016, 5, 197.13, 87.76, 0.63984, 2, 2, 191.01, 94.39, 0.74703, 5, -108.8, 93.45, 0.25296, 2, 2, 497.01, -126.93, 0.36759, 5, 193.02, -133.52, 0.6324, 2, 2, 191.01, -126.93, 0.75447, 5, -112.92, -127.83, 0.24552]
triangles = [10, 11, 12, 13, 12, 14, 12, 2, 14, 11, 0, 1, 14, 2, 3, 12, 1, 2, 11, 1, 12, 14, 3, 4, 15, 14, 4, 7, 5, 6, 8, 15, 7, 7, 15, 5, 9, 13, 8, 8, 13, 15, 9, 10, 13, 15, 4, 5, 13, 14, 15, 10, 12, 13]
        
num_vertex = len(vertices) / 2
num_polygon = 1#len(triangles) / 3
num_polygon_connects = num_vertex * num_polygon#3 * num_polygon

print len(vertices), num_vertex


vertex_array = OpenMaya.MFloatPointArray()
vertex_array.setLength( num_vertex )
vert_counter = 0
for vert_idx in xrange( 0, len(vertices), 2 ):
    print vert_idx, vertices[vert_idx], vertices[vert_idx+1]
    vertex_array.set( OpenMaya.MFloatPoint(vertices[vert_idx], vertices[vert_idx+1], 0), vert_counter )
    vert_counter += 1


polygon_count = OpenMaya.MIntArray()
polygon_count.setLength( num_polygon )
for poly_idx in xrange( 0, num_polygon ):
    polygon_count.set( num_vertex, poly_idx )

polygon_connects = OpenMaya.MIntArray()
polygon_connects.setLength( num_polygon_connects )
#for tri_idx in xrange( 0, len(triangles), 3 ):
#    print triangles[tri_idx], tri_idx
#    print triangles[tri_idx+1], tri_idx+1
#    print triangles[tri_idx+2], tri_idx+2 
#    polygon_connects.set( triangles[tri_idx], tri_idx )
#    polygon_connects.set( triangles[tri_idx+1], tri_idx+1 )
#    polygon_connects.set( triangles[tri_idx+2], tri_idx+2 )
for tri_idx in xrange( 0, vertex_array.length() ):
    polygon_connects.set( tri_idx, tri_idx )

mesh_fn = OpenMaya.MFnMesh()
mesh_fn.create( num_vertex, num_polygon, vertex_array, polygon_count, polygon_connects )